package com.rpc.provider.configer;

import com.rpc.common.exception.RPCConfigException;
import lombok.Getter;
import lombok.Setter;
import org.apache.logging.log4j.util.PropertiesUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@Setter
@Getter
/**
 * rpc的配置信息
 * 提供了默认配置
 */
public class PublishConfiger {
    //本地服务默认端口号
    private int port = 8000;
    private int minPoolSize = 5;
    private int maxPoolSize = 10;
    private String hostAddress;
    private String defaultVersion="1.0.0";
    private String serverAddress;
    //服务注册中心默认端口号
    private int serverPort=9000;
    private Properties properties;
    public PublishConfiger(String filePath) {
        //使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(filePath);
        //使用properties对象加载输入流
        properties=new Properties();
        try {
            properties.load(in);
            initProperties();
        } catch (IOException e) {
            System.out.println("加载配置文件失败");
            e.printStackTrace();
        }
    }

    /**
     * 初始化配置信息
     * @throws RPCConfigException 本机的服务发布方的地址和服务注册中心的地址必须给出，否则会抛出异常
     */
    private void initProperties() throws RPCConfigException {
        String localPort=properties.getProperty("localPort");
        if(localPort!=null&&!localPort.isEmpty()){
            port=Integer.parseInt(localPort);
        }

        String hostAddress1=properties.getProperty("localHostAddress");
        if(hostAddress1!=null&&!hostAddress1.isEmpty()){
            hostAddress=hostAddress1;
        }else{
            throw new RPCConfigException("配置文件缺少本地提供服务的地址信息");
        }

        String minPoolSize1=properties.getProperty("minPoolSize");
        if(minPoolSize1!=null&&!minPoolSize1.isEmpty()){
            minPoolSize=Integer.parseInt(minPoolSize1);
        }

        String maxPoolSize1=properties.getProperty("maxPoolSize");
        if(maxPoolSize1!=null&&!maxPoolSize1.isEmpty()){
            maxPoolSize=Integer.parseInt(maxPoolSize1);
        }

        String defaultVersion1=properties.getProperty("defaultVersion");
        if(defaultVersion1!=null&&!defaultVersion1.isEmpty()){
            defaultVersion=defaultVersion1;
        }

        String serverAddress1=properties.getProperty("serverAddress");
        if(serverAddress1!=null&&!serverAddress1.isEmpty()){
            serverAddress=serverAddress1;
        }else{
            throw new RPCConfigException("服务注册中心的地址未配置");
        }

        String serverPort1=properties.getProperty("serverPort");
        if(serverPort1!=null&&!serverPort1.isEmpty()){
            serverPort=Integer.parseInt(serverPort1);
        }
    }

    public int getPort() {
        String localPort=properties.getProperty("localPort");
        if(localPort!=null&&!localPort.isEmpty()){
            port=Integer.parseInt(localPort);
        }
        return port;
    }

    public int getMinPoolSize() {

        return minPoolSize;
    }

    public int getMaxPoolSize() {

        return maxPoolSize;
    }

    public String getHostAddress() {

        return hostAddress;
    }

    public String getDefaultVersion() {
        return defaultVersion;
    }

    public String getServerAddress() {
        return serverAddress;
    }

    public int getServerPort() {
        return serverPort;
    }

}
